{$MODE objfpc}
unit Emb;

interface
  uses PythonTypes, PythonModSupport, PythonMethodObject;
  
  function emb_myhello(self, args: PyObjectPtr): PyObjectPtr; cdecl;
  procedure RegisterPythonExtension();
  
  function NewMethod(ml_name: PChar; ml_meth: PyCFunction; ml_flags: Integer; ml_doc: PChar): PyMethodDef;
  
implementation

  function NewMethod(ml_name: PChar; ml_meth: PyCFunction; ml_flags: Integer; ml_doc: PChar): PyMethodDef;
  begin
    result.ml_name := ml_name;
    result.ml_meth := ml_meth;
    result.ml_flags := ml_flags;
    result.ml_doc := ml_doc;
  end;

function emb_myhello(self, args: PyObjectPtr): PyObjectPtr; cdecl;
begin
  WriteLn('here');
  result := Py_BuildValue('i', [ 1234 ]);
  WriteLn('end here');
end;

procedure RegisterPythonExtension();
var
  m: PyCFunction;
  data: Array of PyMethodDef;  
begin
  SetLength(data, 2);
  m := @emb_myhello;
  
  data[0] := NewMethod('myhello', m, METH_VARARGS, 'Blah... docs');
  data[1] := NewMethod(nil, nil, 0, nil);

  Py_InitModule('emb', @data[0]);
  // static PyMethodDef EmbMethods[] = {
  //     {"numargs", emb_numargs, METH_VARARGS,
  //      "Return the number of arguments received by the process."},
  //     {NULL, NULL, 0, NULL}
end;

end.